/*
package com.omega.encryptedsql.utils;

*/
/**
 * @author omega
 * @description: TODO
 * @date 2025/7/12
 *//*

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;

public class MyBatisParamResolver {

    */
/**
     * 从 PreparedStatement 获取 MyBatis 参数对象
     *//*

    public static Object getParameterObject(PreparedStatement ps) {
        try {
            // 1. 获取真实 Statement 对象
            Object realStatement = getRealStatement(ps);

            // 2. 获取 StatementHandler
            StatementHandler handler = getStatementHandler(realStatement);

            // 3. 获取 ParameterHandler
            ParameterHandler parameterHandler = handler.getParameterHandler();

            // 4. 获取参数对象
            return parameterHandler.getParameterObject();
        } catch (Exception e) {
            throw new RuntimeException("获取参数对象失败", e);
        }
    }

    */
/**
     * 获取真实 Statement 对象（处理代理）
     *//*

    private static Object getRealStatement(PreparedStatement ps) throws Exception {
        // 如果不是代理对象，直接返回
        if (!Proxy.isProxyClass(ps.getClass())) {
            return ps;
        }

        // 获取代理的 InvocationHandler
        InvocationHandler ih = Proxy.getInvocationHandler(ps);

        // 处理 MyBatis 的 PreparedStatementLogger
        if (ih.getClass().getName().contains("PreparedStatementLogger")) {
            Field statementField = getField(ih.getClass(), "statement");
            statementField.setAccessible(true);
            return statementField.get(ih);
        }

        // 处理其他代理类型
        Field delegateField = getField(ps.getClass(), "delegate");
        if (delegateField != null) {
            delegateField.setAccessible(true);
            return delegateField.get(ps);
        }

        return ps;
    }

    */
/**
     * 获取 StatementHandler
     *//*

    private static StatementHandler getStatementHandler(Object statement) throws Exception {
        // 尝试获取 RoutingStatementHandler 中的 delegate
        if (statement.getClass().getName().contains("RoutingStatementHandler")) {
            MetaObject metaObject = SystemMetaObject.forObject(statement);
            return (StatementHandler) metaObject.getValue("delegate");
        }

        // 尝试获取 delegate 字段
        Field delegateField = getField(statement.getClass(), "delegate");
        if (delegateField != null) {
            delegateField.setAccessible(true);
            Object delegate = delegateField.get(statement);
            if (delegate instanceof StatementHandler) {
                return (StatementHandler) delegate;
            }
        }

        // 尝试获取 statementHandler 字段
        Field handlerField = getField(statement.getClass(), "statementHandler");
        if (handlerField != null) {
            handlerField.setAccessible(true);
            return (StatementHandler) handlerField.get(statement);
        }

        throw new IllegalStateException("无法获取 StatementHandler");
    }

    */
/**
     * 安全获取字段（包含父类查找）
     *//*

    private static Field getField(Class<?> clazz, String fieldName) {
        Class<?> searchType = clazz;
        while (searchType != null) {
            try {
                Field field = searchType.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field;
            } catch (NoSuchFieldException e) {
                searchType = searchType.getSuperclass();
            }
        }
        return null;
    }

    */
/**
     * 获取当前 BoundSql 对象
     *//*

    public static BoundSql getBoundSql(PreparedStatement ps) {
        try {
            Object realStatement = getRealStatement(ps);
            StatementHandler handler = getStatementHandler(realStatement);
            return handler.getBoundSql();
        } catch (Exception e) {
            throw new RuntimeException("获取 BoundSql 失败", e);
        }
    }

    */
/**
     * 获取当前参数映射
     *//*

    public static ParameterMapping getParameterMapping(PreparedStatement ps, int index) {
        try {
            BoundSql boundSql = getBoundSql(ps);
            return boundSql.getParameterMappings().get(index - 1);
        } catch (Exception e) {
            throw new RuntimeException("获取参数映射失败", e);
        }
    }

    */
/**
     * 获取当前处理的字段名
     *//*

    public static String getCurrentFieldName(PreparedStatement ps, int paramIndex) {
        try {
            ParameterMapping mapping = getParameterMapping(ps, paramIndex);
            return mapping.getProperty();
        } catch (Exception e) {
            return "unknown_field_" + paramIndex;
        }
    }
}*/
